package cn.fotoit.product.util;

import com.alibaba.fastjson.JSONObject;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.util.StringUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


/**
 * html 生成
 */
public class HtmlGenerate {

    private static String rootUrl = new File("").getAbsolutePath() + "/src/main/";

    public static void main(String[] args) throws Exception {
        //项目根路径
        //模块包路径
        String modulesPackage = "cn.fotoit.product.app1";
        List<Class> classes = ClassUtil.doScan(modulesPackage + ".entity");
        for (Class aClass : classes) {
            List<JSONObject> fieldList = new ArrayList<>();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", declaredField.getName());
                jsonObject.put("type", declaredField.getType().getSimpleName());
                fieldList.add(jsonObject);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("className", StringUtils.uncapitalize(aClass.getSimpleName()));
            jsonObject.put("fieldList", fieldList);
            createListHtml(jsonObject);
            createEditHtml(jsonObject);
        }
        //缓存名前缀

    }


    /**
     * 根据String模板生成HTML，模板中存在List循环
     */
    public static void createListHtml(JSONObject jsonObject) throws Exception {
        File d = new File(rootUrl + "webapp/app1/");
        if (d.exists()) {
        } else {
            d.mkdir();
        }

        //模板文件
        //构造输入流
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(rootUrl + "resources/htmlTemplate/list.ftl"));
        int len;
        byte[] bytes = new byte[1024];
        //模板内容
        StringBuilder content = new StringBuilder();
        while ((len = in.read(bytes)) != -1) {
            content.append(new String(bytes, 0, len, "utf-8"));
        }

        //构造Configuration
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        //构造StringTemplateLoader
        StringTemplateLoader loader = new StringTemplateLoader();
        //添加String模板
        loader.putTemplate("test", content.toString());
        //把StringTemplateLoader添加到Configuration中
        configuration.setTemplateLoader(loader);

        //构造Model

        //获取模板
        Template template = configuration.getTemplate("test");
        //构造输出路
        FileWriter out = new FileWriter(rootUrl + "webapp/app1/" + jsonObject.get("className") + "List.html");
        //生成HTML
        template.process(jsonObject, out);

        in.close();
        out.close();
    }

    public static void createEditHtml(JSONObject jsonObject) throws Exception {
        File d = new File(rootUrl + "webapp/app1/");
        if (d.exists()) {
        } else {
            d.mkdir();
        }

        //模板文件
        File file = new File(rootUrl + "resources/htmlTemplate/edit.ftl");
        //构造输入流
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        int len;
        byte[] bytes = new byte[1024];
        //模板内容
        StringBuilder content = new StringBuilder();
        while ((len = in.read(bytes)) != -1) {
            content.append(new String(bytes, 0, len, "utf-8"));
        }

        //构造Configuration
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        //构造StringTemplateLoader
        StringTemplateLoader loader = new StringTemplateLoader();
        //添加String模板
        loader.putTemplate("test", content.toString());
        //把StringTemplateLoader添加到Configuration中
        configuration.setTemplateLoader(loader);

        //构造Model

        //获取模板
        Template template = configuration.getTemplate("test");
        //构造输出路
        FileWriter out = new FileWriter(rootUrl + "webapp/app1/" + jsonObject.get("className") + "Edit.html");
        //生成HTML
        template.process(jsonObject, out);


        in.close();


        out.close();


    }

}
